package com.maolong.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.maolong.mapper.MenuMapper;
import com.maolong.mapper.RoleMapper;
import com.maolong.mapper.RoleRightMapper;
import com.maolong.mapper.UserMapper;
import com.maolong.pojo.entity.Menu;
import com.maolong.pojo.vo.MenuNodeVo;
import com.maolong.pojo.vo.MenuVo;
import com.maolong.service.MenuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class MenuServiceImpl extends ServiceImpl<MenuMapper,Menu> implements MenuService  {
    @Autowired
    MenuMapper menuMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    RoleMapper roleMapper;
    @Autowired
    RoleRightMapper roleMenuMapper;

    /**
     * 查询所有的菜单信息
     * @return
     */
    @Override
    public List<MenuVo> selectMenus() {

//
//
//        //MDC获取登录id
//        Integer userId = Integer.valueOf(MDC.get(ResultConstant.USER_ID));
//        //根据登录id查询角色名称, TODO 这里数据库表设计有问题，应该搞一个多表的
//        User user = userMapper.selectById(userId);
//        String roleName = user.getRoleName();
//        //根据角色名称查询角色id
//        Role role = roleMapper.selectOne(new QueryWrapper<Role>().eq("role_name", roleName));
//        //TODO 这里数据库定义字段也不对，怎么能role_id呢。应该是id
//        Integer roleId = role.getRoleId();
//        //根据角色id查询菜单id
//        List<RoleMenu> roleMenus = roleMenuMapper.selectList(new QueryWrapper<RoleMenu>().eq("role_id", roleId));
//        //根据菜单id查询菜单
//        List<Menu> allMenus = menuMapper.selectList(new QueryWrapper<Menu>()
//                .in("id", roleMenus.stream()
//                        .map(RoleMenu -> RoleMenu.getMenuId()).collect(Collectors.toList())));


        List<Menu> allMenus = menuMapper.selectMenus();
        List<MenuVo> parentMenuVos = new ArrayList<>();
        List<MenuVo> childMenuVos = new ArrayList<>();

        //筛选出父菜单和子菜单
        allMenus.forEach(menu -> {
            MenuVo menuVo = new MenuVo();
            BeanUtils.copyProperties(menu,menuVo);
            if (menu.getPId()==null)
                parentMenuVos.add(menuVo);
            childMenuVos.add(menuVo);
        });
        //将父菜单排序
        parentMenuVos.stream().sorted(Comparator.comparing(MenuVo::getOrderValue)).collect(Collectors.toList());
        //递归查找儿子
        parentMenuVos.forEach(parentMenuVo -> {
            List<MenuVo> childs = getChild(parentMenuVo.getId(), childMenuVos);
            List<MenuVo> collect = childs.stream().sorted(Comparator.comparing(MenuVo -> MenuVo.getOrderValue())).collect(Collectors.toList());
            parentMenuVo.setMenus(collect);
        });

        return parentMenuVos;
    }

    /**
     * 返回父节点信息
     * @return
     */
    @Override
    public List<MenuNodeVo> selectNodes() {
        List<MenuNodeVo> menus = menuMapper.selectNodes();
        return menus;
    }



    @Override
    public void delete(Integer id) {
        menuMapper.deleteWithPid(id);
    }

    private List<MenuVo> getChild(Long menuId,List<MenuVo> allMenus){
        List<MenuVo> childMenus = new ArrayList<>();
        //找第一层儿子
        allMenus.forEach(menuVo -> {
            if (menuVo.getPId()!=null && menuVo.getPId()==menuId){
                 childMenus.add(menuVo);
            }
        });

        if(childMenus.size()==0)
            return childMenus;

        //找儿子儿子
        childMenus.forEach(menuVo -> {
            if(StringUtils.isEmpty(menuVo.getUrl())){
                getChild(menuVo.getId(),allMenus);
            }
        });

        return childMenus;
    }
}
